package top.tttfu.dao;

import top.tttfu.entity.Task;
import top.tttfu.entity.User;
import top.tttfu.util.JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TaskDaoImpl implements ITaskDao{

    @Override
    public int addTask(Connection con, Task task) {
        String sql = "insert into task (content, emailPush, finished, userAccount, date) values (?,?,?,?,?)";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        int id = 0;
        try {
            pStatement = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            User user = new User();
            pStatement.setString(1, task.getContent());
            pStatement.setBoolean(2,task.isEmailPush());
            pStatement.setBoolean(3,task.isFinished());
            pStatement.setString(4,task.getUserAccount());
            pStatement.setTimestamp(5,new Timestamp(task.getDate().getTime()));
            pStatement.executeUpdate();
            rs = pStatement.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return id;
    }

    @Override
    public Task selectTaskByID(Connection con, int id) {

        return null;
    }

    @Override
    public List<Task> selectTaskByUser(Connection con, User user) {
        List<Task> list = new ArrayList<>();

        String sql = "select * from task where userAccount = (?)";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        int id = 0;
        try {
            pStatement = con.prepareStatement(sql);
            pStatement.setString(1,user.getAccount());
            rs = pStatement.executeQuery();
            while (rs.next()){
               list.add(new Task(rs.getInt(1),
                       rs.getString(5),
                       rs.getString(2),
                       rs.getTimestamp(6),
                       rs.getBoolean(3),
                       rs.getBoolean(4)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return  list;
    }

    @Override
    public boolean updateTask(Connection con, Task task) {
        String sql = "update task set content = (?) ,emailPush = (?), finished = (?), date = (?) where id = (?) ";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            pStatement.setString(1, task.getContent());
            pStatement.setBoolean(2,task.isEmailPush());
            pStatement.setBoolean(3,task.isFinished());
            pStatement.setTimestamp(4,new Timestamp(task.getDate().getTime()));
            pStatement.setInt(5,task.getId());
            pStatement.executeUpdate();
            if(pStatement.getUpdateCount() == 0){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return true;
    }

    @Override
    public boolean deleteTaskByID(Connection con, int id) {

        String sql = "delete from task where id = (?)";
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = con.prepareStatement(sql);
            pStatement.setInt(1,id);
            pStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            JDBC.close(null, pStatement, rs);
        }
        return true;
    }
}
